package com.ssm.service.impl;

import com.ssm.bean.Employee;
import com.ssm.bean.EmployeeExample;
import com.ssm.dao.EmployeeMapper;
import com.ssm.service.interfaces.IEmployeeSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright: Copyright (c) 2018/1/2
 * Description:
 * CLASS_NAME: EmployeeSVImpl
 * PACKAGE_NAME: com.ssm.service.impl
 * version: v1.0.0
 * author: Jin Biao
 * date: 2018/1/2 19:07
 */
@Service
public class EmployeeSVImpl implements IEmployeeSV{
    @Autowired
    EmployeeMapper employeeMapper;

    public List<Employee> getAllEmployee() throws Exception {
        return employeeMapper.selectByExample(null);
    }

    public List<Employee> getAllEmployeeWithDept() throws Exception {
        return employeeMapper.selectByExampleWithDept(null);
    }

    public void saveEmployee(Employee employee) throws Exception {
        employeeMapper.insertSelective(employee);
    }

    public boolean checkEmployee(String empName) throws Exception {
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(employeeExample);
        return count == 0;
    }

    @Override
    public Employee getEmpById(int empId) throws Exception {
       return employeeMapper.selectByPrimaryKey(empId);
    }

    @Override
    public void updateEmployee(Employee employee){
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    @Override
    public void deleteEmpById(int empId) {
        employeeMapper.deleteByPrimaryKey(empId);
    }

    @Override
    public void deleteEmpBath(String[] empIds) {
        List<Integer> empIdList = new ArrayList<>();
        for(String id : empIds){
            empIdList.add(Integer.parseInt(id));
        }
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        //delete from tableName where emp_id in();
        criteria.andEmpIdIn(empIdList);
        employeeMapper.deleteByExample(employeeExample);
    }
}
